<?php
class Tourism_order_model extends BZ_Model
{
    protected $table   = 'tourism_order';
    protected $table_2 = 'tourism_order_product';
    protected $table_3 = 'user';
    protected $table_4 = 'tourism_delivery_address';
    protected $table_5 = 'tourism_order_reviews';

    public function findByOrderId($order_id)
    {
        $this->db->where('tourism_order.order_id',$order_id);
        return $this->db->get($this->table);
    }
    
    /**
     * 具体发货信息
     * @param unknown $order_id
     */
    public function findDeliveryByOrderId($order_id){

        $this->db->select('tourism_order.*,tourism_order_product.goods_name,tourism_delivery_address.tel');
        $this->db->from($this->table);
        $this->db->join($this->table_2.' AS tourism_order_product','tourism_order_product.order_id = tourism_order.order_id');
        $this->db->join($this->table_4.' AS tourism_delivery_address','tourism_delivery_address.address_id = tourism_order.address_id','left');
        $this->db->where('tourism_order.order_id',$order_id);
        return $this->db->get();
    }
    
    public function total($param = array())
    {
        $this->db->select('COUNT(*) as count');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2.' AS tourism_order_product','tourism_order_product.order_id = tourism_order.order_id');
        if (!empty($param['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $param['seller_uid']);
        }
        if (!empty($param['order_status'])) {
            $this->db->where('tourism_order.order_status', $param['order_status']);
        }
        if (!empty($param['order_search'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$param['order_search']}%') OR (`tourism_order`.`order_sn` = '{$param['order_search']}')");
        }
        if (!empty($param['start_date'])) {
            $this->db->where('tourism_order.created_at >', $param['start_date'] . ' 00:00:00');
        }
        if (!empty($param['end_date'])) {
            $this->db->where('tourism_order.created_at <', $param['end_date'] . ' 23:59:59');
        }
        $this->db->group_by('tourism_order.order_id');
        $result = $this->db->get();
        return $result->num_rows();
    }
    
    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('tourism_order.*');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2.' AS tourism_order_product','tourism_order_product.order_id = tourism_order.order_id');
        if (!empty($param['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $param['seller_uid']);
        }
        if (!empty($param['order_status'])) {
            $this->db->where('tourism_order.order_status', $param['order_status']);
        }
        if (!empty($param['order_search'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$param['order_search']}%') OR (`tourism_order`.`order_sn` = '{$param['order_search']}')");
        }
        if (!empty($param['start_date'])) {
            $this->db->where('tourism_order.created_at >', $param['start_date'] . ' 00:00:00');
        }
        if (!empty($param['end_date'])) {
            $this->db->where('tourism_order.created_at <', $param['end_date'] . ' 23:59:59');
        }
        $this->db->group_by('tourism_order.order_id');
        $this->db->order_by('tourism_order.order_id', 'DESC');
        if( empty($param['excel'])){
            $this->db->limit($page_num, $num);
        }
        return $this->db->get();
    }
    
    public function page_lists($params)
    {
        if (empty($params['ids'])) {
            return false;
        }
        $this->db->select('tourism_order.*,tourism_order_product.order_product_id,tourism_order_product.goods_attr_id,tourism_order_product.goods_name,tourism_order_product.goods_image,tourism_order_product.attribute_value,tourism_order_product.number,tourism_order_product.barter_num,tourism_order_product.refund_num,tourism_order_product.shop_price,tourism_order_product.supply_price,tourism_order_product.pay_amount,tourism_order_reviews.id');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2.' AS tourism_order_product','tourism_order_product.order_id = tourism_order.order_id');
        $this->db->join($this->table_5 . ' AS tourism_order_reviews', 'tourism_order_reviews.order_product_id = tourism_order_product.order_product_id','left');
        if (!empty($params['ids'])) {
            $this->db->where_in('tourism_order.order_id', $params['ids']);
        }
        $this->db->where('tourism_order_product.refund_num >=','0');
        $this->db->order_by('tourism_order.order_id', 'DESC');
        return $this->db->get();
    }
    
     /**
     *  下载订单所需要的字段
     * @param unknown $params
     */
    public function page_list_down($params){
        
        if (empty($params['ids'])) {
            return false;
        }
        $this->db->select('tourism_order.order_sn,user.alias_name,tourism_order_product.goods_name,tourism_order_product.attribute_value,tourism_order_product.refund_num,tourism_order_product.supply_price,tourism_order.detail,tourism_order.message');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2.' AS tourism_order_product','tourism_order_product.order_id = tourism_order.order_id');
        $this->db->join($this->table_3. ' AS user', 'user.uid = tourism_order.payer_uid');
        //$this->db->join($this->table_4.' AS tourism_delivery_address','tourism_delivery_address.address_id = tourism_order.address_id','left');
        if (!empty($params['ids'])) {
            $this->db->where_in('tourism_order.order_id', $params['ids']);
        }
        $this->db->where('tourism_order_product.refund_num >','0');
        $this->db->order_by('tourism_order.order_id', 'DESC');
        return $this->db->get();
    }
    
    public function orderStatusTotal($param = array(), $state = false)
    {
        $this->db->select('tourism_order.order_status, count(tourism_order.order_status) AS status_count');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_3 . ' AS user', 'user.uid = tourism_order.payer_uid');
        if (!empty($param['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $param['seller_uid']);
        }
        if (!empty($param['start_date'])) {
            $this->db->where('tourism_order.created_at >', $param['start_date'] . ' 00:00:00');
        }
        if (!empty($param['end_date'])) {
            $this->db->where('tourism_order.created_at <', $param['end_date'] . ' 23:59:59');
        }
        $this->db->group_by('tourism_order.order_status');
        $result = $this->db->get();
        if ($state) {
            $rows = array();
            if ($result->num_rows() > 0) {
                foreach ($result->result_array() as $row) {
                    $rows[$row['order_status']] = $row['status_count'];
                }
            }
            return $rows;
        }
        return $result;
    }
    
    /**
     * 供应商手动取消订单
     * @param unknown $params
     */
    public function UndoNotPayOrder($order_ids=array())
    {
        $data = array(
            'order_state'  => 1, //未付款
            'order_status' => 1, //取消订单
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where_in('order_id', $order_ids);
        $this->db->where('order_status', 2);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 供应商发货，修改订单状态
     * @param unknown $params
     */
    public function sendTourismOrder($params)
    {
        $data = array(
            'order_state'  => 2, //已付款
            'order_status' => 4, //已发货
            'send_time'    => date('Y-m-d H:i:s', time()),
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        if (!empty($params['logistics'])) {
            $data['logistics'] = $params['logistics'];
        }
        if (!empty($params['logistics_company'])) {
            $data['logistics_company'] = $params['logistics_company'];
        }
        $this->db->where('order_id', $params['order_id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 修改订单状态
     * @param unknown $params
     */
    public function updateOrderStatus($params)
    {
        $data = array(
            'order_state'  => $params['order_state'],
            'order_status' => $params['order_status'],
            'updated_at'   => date('Y-m-d H:i:s', time()),
        );
        $this->db->where('order_id', $params['order_id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 修改运费
     * @param unknown $order_id
     * @param unknown $transport_cost
     */
    public function modifyTransport($order_id, $transport_cost)
    {
        $data = array(
                'transport_cost' => $transport_cost
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 获取要更改的信息
     * @param unknown $params
     */
    public function findByParamsOrder($params)
    {
        if (!empty($params['order_status'])) {
            $this->db->where('order_status', $params['order_status']);
        }
        if (!empty($params['created_at'])) { //生成订单时间
            $this->db->where('created_at <', $params['created_at']);
        }
        if (!empty($params['send_time'])) { //卖家发货时间
            $this->db->where('send_time <', $params['send_time']);
        }
        if (!empty($params['receive_time'])) { //买家确认收货时间
            $this->db->where('receive_time <', $params['receive_time']);
        }
        return $this->db->get($this->table);
    }
    
    /**
     * 发货后7天未确认收货的，订单自动确认收货
     */
    public function receiveTourismOrder($order_ids)
    {
        $data = array(
            'order_state'  => 3, //已完成
            'order_status' => 5, //已收货
            'receive_time' => date('Y-m-d H:i:s', time()),
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where_in('order_id', $order_ids);
        $this->db->where('order_status', 4);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 确认收货后7天未评价的，订单自动评价，分钱
     */
    public function reviewsTourismOrder($order_ids)
    {
        $data = array(
            'order_state'  => 4, //已评价
            'order_status' => 6, //已评价
            'reviews_time' => date('Y-m-d H:i:s', time()),
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where_in('order_id', $order_ids);
        $this->db->where('order_status', 5);
        return $this->db->update($this->table, $data);
    }
}